<?php
/**
 * Created by PhpStorm.
 * User: hjl
 * Date: 2016/8/31
 * Time: 13:46
 */

namespace Admin\Controller;

use Admin\Extend\Page;

/**
 * Class 可疑人员管理
 * @package Admin\Controller
 */
class IndexController extends BaseController
{

    public function index($key = "")
    {

        if ($key === "") {
            $model = D('suspicious');
        } else {
            $condition['name'] = array('like', "$key");
            $condition['sfzh'] = array('like', "$key");
            $condition['_logic'] = 'or';
            $model = D('suspicious')->where($condition);
        }

        /* 查询满足要求的总记录数 */
        $count = $model->where($condition)->count();

        /* 实例化分页类 传入总记录数和每页显示的记录数(15) */
        $page = new Page($count, 15);

        /* 分页显示输出 */
        $show = $page->show();
        $post = $model->limit($page->firstRow . ',' . $page->listRows)->where($condition)->order('id DESC')->select();
        $this->assign('model', $post);
        $this->assign('page', $show);
        $this->display();
    }

    public function changestatus()
    {
        if (IS_POST) {
            $id = I('post.id');
            $status = I('post.status');
        } else if (IS_GET) {
            $id = I('get.id');
            $status = I('get.status');
        }

        $condition['id'] = $id;
        $updataData['bk_status'] = $status;
        $result = D('suspicious')->where($condition)->save($updataData);

        if ($result != null) {
            $data["status"] = true;
        } else {
            $data["status"] = false;
        }
        $this->ajaxReturn($data, "json");
    }

    public function del(){
        if (IS_POST) {
            $id = I('post.id');
        } else if (IS_GET) {
            $id = I('get.id');
        }

        $condition['id'] = $id;
        $result = D('suspicious')->where($condition)->delete();

        if ($result != null) {
            $data["status"] = true;
        } else {
            $data["status"] = false;
        }
        $this->ajaxReturn($data, "json");
    }


    public function single()
    {
        $this->display("Index:single");
    }

    public function singleimport()
    {
        if (!IS_POST) {
            $this->display();
        }

        if (IS_POST) {
            $model = D("suspicious");
            if (!$model->create()) {
                /* 如果创建失败 表示验证没有通过 输出错误提示信息 */
                $this->error($model->getError());
                exit();
            } else {
                if ($model->add()) {
                    redirect(U('Index/index'));
                } else {
                    $this->error("添加失败");
                }
            }
        }
    }

    public function multi()
    {
        $this->display("Index:multi");
    }

    /**
     * 查看详情
     */
    public function info($key = '')
    {
        $condition['id'] = $key;
        $info = M("suspicious")->where($condition)->find();
        $this->assign('info', $info);
        $this->display("Index:info");
    }


    public function multiimport()
    {
        if (!empty($_FILES)) {
            $config = array(
                'exts' => array('xlsx', 'xls'),
                'maxSize' => 3145728,
                'rootPath' => FileRootPath,
                'savePath' => FilePath,
            );

            $upload = new \Think\Upload($config);
            if (!$info = $upload->upload()) {
                $this->error($upload->getError());
            }
            $values = array_values($info);
            vendor("PHPExcel.PHPExcel");
            $file_name = $upload->rootPath . $values[0]['savepath'] . $values[0]['savename'];
            $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

            if ($extension == 'xlsx') {
                $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
                $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            } else if ($extension == 'xls') {
                $objReader = \PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            }

            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow();//取得总行数
//            $highestColumn = $sheet->getHighestColumn(); //取得总列数

            for ($i = 2; $i <= $highestRow; $i++) {
                $data['name'] = $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
                $data['sex'] = $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
                $data['mz'] = $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
                $data['sfzh'] = $objPHPExcel->getActiveSheet()->getCell("D" . $i)->getValue();
                $data['resident'] = $objPHPExcel->getActiveSheet()->getCell("E" . $i)->getValue();
                $data['resident_current'] = $objPHPExcel->getActiveSheet()->getCell("F" . $i)->getValue();
                $data['type'] = $objPHPExcel->getActiveSheet()->getCell("G" . $i)->getValue();
                $data['risk_grade'] = $objPHPExcel->getActiveSheet()->getCell("H" . $i)->getValue();
                $data['bk_status'] = $objPHPExcel->getActiveSheet()->getCell("I" . $i)->getValue();
                $data['polices'] = $objPHPExcel->getActiveSheet()->getCell("J" . $i)->getValue();
                $data['p_type'] = $objPHPExcel->getActiveSheet()->getCell("K" . $i)->getValue();
                $data['p_dept'] = $objPHPExcel->getActiveSheet()->getCell("L" . $i)->getValue();
                $data['p_tel'] = $objPHPExcel->getActiveSheet()->getCell("M" . $i)->getValue();
                $data['postscript'] = $objPHPExcel->getActiveSheet()->getCell("N" . $i)->getValue();
                $data['opt_name'] = session("username");

                $time = time();
                $data['bk_at'] = $time;
                $data['opt_at'] = $time;

                M('suspicious')->add($data);
            }

            $this->apiReturn(200, '上传成功', null, 'json');
        } else {
            $this->apiReturn(400, '请选择上传的文件', null, 'json');
        }
    }


}